home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-orca / orca / speechserver.py < prev    next >
Encoding:
Python Source  |  2009-04-13  |  9.8 KB  |  311 lines

  1. # Orca
  2. #
  3. # Copyright 2005-2008 Sun Microsystems Inc.
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Library General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2 of the License, or (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # Library General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Library General Public
  16. # License along with this library; if not, write to the
  17. # Free Software Foundation, Inc., Franklin Street, Fifth Floor,
  18. # Boston MA  02110-1301 USA.
  19.  
  20. """Provides an abtract class for working with speech servers.
  21.  
  22. A speech server (class SpeechServer) provides the ability to tell the
  23. machine to speak.  Each speech server provides a set of known
  24. voices (identified by name) which can be combined with various
  25. attributes to create aural style sheets."""
  26.  
  27. __id__        = "$Id: speechserver.py 4467 2009-01-27 16:40:00Z wwalker $"
  28. __version__   = "$Revision: 4467 $"
  29. __date__      = "$Date: 2009-01-27 11:40:00 -0500 (Tue, 27 Jan 2009) $"
  30. __copyright__ = "Copyright (c) 2005-2008 Sun Microsystems Inc."
  31. __license__   = "LGPL"
  32.  
  33. import logging
  34. import keynames
  35. import settings
  36. import orca
  37. import orca_state
  38.  
  39. log = logging.getLogger("speech")
  40.  
  41. import debug
  42.  
  43. from orca_i18n import _           # for gettext support
  44.  
  45. class VoiceFamily(dict):
  46.     """Holds the family description for a voice."""
  47.  
  48.     NAME   = "name"
  49.     GENDER = "gender"
  50.     LOCALE = "locale"
  51.  
  52.     MALE   = "male"
  53.     FEMALE = "female"
  54.  
  55.     settings = {
  56.         NAME   : None,
  57.         GENDER : None,
  58.         LOCALE : None
  59.     }
  60.  
  61.     def __init__(self, props):
  62.         """Create and initialize VoiceFamily."""
  63.         dict.__init__(self)
  64.  
  65.         self.update(VoiceFamily.settings)
  66.         if props:
  67.             self.update(props)
  68.  
  69. class SayAllContext:
  70.  
  71.     PROGRESS    = 0
  72.     INTERRUPTED = 1
  73.     COMPLETED   = 2
  74.  
  75.     def __init__(self, obj, utterance, startOffset=-1, endOffset=-1):
  76.         """Creates a new SayAllContext that will be passed to the
  77.         SayAll callback handler for progress updates on speech.
  78.         If the object does not have an accessible text specialization,
  79.         then startOffset and endOffset parameters are meaningless.
  80.         If the object does have an accessible text specialization,
  81.         then values >= 0 for startOffset and endOffset indicate
  82.         where in the text the utterance has come from.
  83.  
  84.         Arguments:
  85.         -obj:         the Accessible being spoken
  86.         -utterance:   the actual utterance being spoken
  87.         -startOffset: the start offset of the Accessible's text
  88.         -endOffset:   the end offset of the Accessible's text
  89.         """
  90.         self.obj           = obj
  91.         self.utterance     = utterance
  92.         self.startOffset   = startOffset
  93.         self.currentOffset = startOffset
  94.         self.endOffset     = endOffset
  95.  
  96.  
  97. class SpeechServer(object):
  98.  
  99.     """Provides speech server abstraction."""
  100.  
  101.     def getFactoryName():
  102.         """Returns a localized name describing this factory."""
  103.         pass
  104.  
  105.     getFactoryName = staticmethod(getFactoryName)
  106.  
  107.     def getSpeechServers():
  108.         """Gets available speech servers as a list.  The caller
  109.         is responsible for calling the shutdown() method of each
  110.         speech server returned.
  111.         """
  112.         pass
  113.  
  114.     getSpeechServers = staticmethod(getSpeechServers)
  115.  
  116.     def getSpeechServer(info):
  117.         """Gets a given SpeechServer based upon the info.
  118.         See SpeechServer.getInfo() for more info.
  119.         """
  120.         pass
  121.  
  122.     getSpeechServer = staticmethod(getSpeechServer)
  123.  
  124.     def shutdownActiveServers():
  125.         """Cleans up and shuts down this factory.
  126.         """
  127.         pass
  128.  
  129.     shutdownActiveServers = staticmethod(shutdownActiveServers)
  130.  
  131.     def __init__(self):
  132.         pass
  133.  
  134.     def getInfo(self):
  135.         """Returns [name, id]
  136.         """
  137.         pass
  138.  
  139.     def getVoiceFamilies(self):
  140.         """Returns a list of VoiceFamily instances representing all
  141.         voice families known by the speech server."""
  142.         pass
  143.  
  144.     def queueText(self, text="", acss=None):
  145.         """Adds the text to the queue.
  146.  
  147.         Arguments:
  148.         - text: text to be spoken
  149.         - acss: acss.ACSS instance; if None,
  150.                 the default voice settings will be used.
  151.                 Otherwise, the acss settings will be
  152.                 used to augment/override the default
  153.                 voice settings.
  154.  
  155.         Output is produced by the next call to speak.
  156.         """
  157.         pass
  158.  
  159.     def queueTone(self, pitch=440, duration=50):
  160.         """Adds a tone to the queue.
  161.  
  162.         Output is produced by the next call to speak.
  163.         """
  164.         pass
  165.  
  166.     def queueSilence(self, duration=50):
  167.         """Adds silence to the queue.
  168.  
  169.         Output is produced by the next call to speak.
  170.         """
  171.         pass
  172.  
  173.     def speakCharacter(self, character, acss=None):
  174.         """Speaks a single character immediately.
  175.  
  176.         Arguments:
  177.         - character: text to be spoken
  178.         - acss:      acss.ACSS instance; if None,
  179.                      the default voice settings will be used.
  180.                      Otherwise, the acss settings will be
  181.                      used to augment/override the default
  182.                      voice settings.
  183.         """
  184.         pass
  185.  
  186.     def speakKeyEvent(self, event_string, eventType):
  187.         """Speaks a key event immediately.
  188.  
  189.         Arguments:
  190.         - event_string: string representing the key event as defined by
  191.                         input_event.KeyboardEvent.
  192.         - eventType:    key event type as one of orca.KeyEventType constants.
  193.  
  194.         """
  195.         if eventType == orca.KeyEventType.PRINTABLE and \
  196.                event_string.decode("UTF-8").isupper():
  197.             voice = settings.voices[settings.UPPERCASE_VOICE]
  198.         else:
  199.             voice = settings.voices[settings.DEFAULT_VOICE]
  200.  
  201.         # Check to see if there are localized words to be spoken for
  202.         # this key event.
  203.         #
  204.         event_string = keynames.getKeyName(event_string)
  205.         if orca_state.activeScript and orca_state.usePronunciationDictionary:
  206.             event_string = \
  207.                 orca_state.activeScript.adjustForPronunciation(event_string)
  208.  
  209.         if eventType == orca.KeyEventType.LOCKING_LOCKED:
  210.             # Translators: this represents the state of a locking modifier
  211.             # key (e.g., Caps Lock)
  212.             #
  213.             event_string += " " + _("on")
  214.         elif eventType == orca.KeyEventType.LOCKING_UNLOCKED:
  215.             # Translators: this represents the state of a locking modifier
  216.             # key (e.g., Caps Lock)
  217.             #
  218.             event_string += " " + _("off")
  219.  
  220.         logLine = "SPEECH OUTPUT: '" + event_string +"'"
  221.         debug.println(debug.LEVEL_INFO, logLine)
  222.         log.info(logLine)
  223.  
  224.         self.speak(event_string, acss=voice)
  225.  
  226.     def speakUtterances(self, utteranceList, acss=None, interrupt=True):
  227.         """Speaks the given list of utterances immediately.
  228.  
  229.         Arguments:
  230.         - utteranceList: list of strings to be spoken
  231.         - acss:      acss.ACSS instance; if None,
  232.                      the default voice settings will be used.
  233.                      Otherwise, the acss settings will be
  234.                      used to augment/override the default
  235.                      voice settings.
  236.         - interrupt: if True, stop any speech currently in progress.
  237.         """
  238.         pass
  239.  
  240.     def speak(self, text=None, acss=None, interrupt=True):
  241.         """Speaks all queued text immediately.  If text is not None,
  242.         it is added to the queue before speaking.
  243.  
  244.         Arguments:
  245.         - text:      optional text to add to the queue before speaking
  246.         - acss:      acss.ACSS instance; if None,
  247.                      the default voice settings will be used.
  248.                      Otherwise, the acss settings will be
  249.                      used to augment/override the default
  250.                      voice settings.
  251.         - interrupt: if True, stops any speech in progress before
  252.                      speaking the text
  253.         """
  254.         pass
  255.  
  256.     def isSpeaking(self):
  257.         """"Returns True if the system is currently speaking."""
  258.         return False
  259.  
  260.     def sayAll(self, utteranceIterator, progressCallback):
  261.         """Iterates through the given utteranceIterator, speaking
  262.         each utterance one at a time.  Subclasses may postpone
  263.         getting a new element until the current element has been
  264.         spoken.
  265.  
  266.         Arguments:
  267.         - utteranceIterator: iterator/generator whose next() function
  268.                              returns a [SayAllContext, acss] tuple
  269.         - progressCallback:  called as speech progress is made - has a
  270.                              signature of (SayAllContext, type), where
  271.                              type is one of PROGRESS, INTERRUPTED, or
  272.                              COMPLETED.
  273.         """
  274.         for [context, acss] in utteranceIterator:
  275.             logLine = "SPEECH OUTPUT: '" + context.utterance + "'"
  276.             debug.println(debug.LEVEL_INFO, logLine)
  277.             log.info(logLine)
  278.             self.speak(context.utterance, acss)
  279.  
  280.     def increaseSpeechRate(self, step=5):
  281.         """Increases the speech rate.
  282.         """
  283.         pass
  284.  
  285.     def decreaseSpeechRate(self, step=5):
  286.         """Decreases the speech rate.
  287.         """
  288.         pass
  289.  
  290.     def increaseSpeechPitch(self, step=0.5):
  291.         """Increases the speech pitch.
  292.         """
  293.         pass
  294.  
  295.     def decreaseSpeechPitch(self, step=0.5):
  296.         """Decreases the speech pitch.
  297.         """
  298.         pass
  299.  
  300.     def stop(self):
  301.         """Stops ongoing speech and flushes the queue."""
  302.         pass
  303.  
  304.     def shutdown(self):
  305.         """Shuts down the speech engine."""
  306.         pass
  307.  
  308.     def reset(self, text=None, acss=None):
  309.         """Resets the speech engine."""
  310.         pass
  311.